package com.example.rightmost.service.Impl;

import com.example.rightmost.dao.PieceLikeMapper;
import com.example.rightmost.dao.PieceMapper;
import com.example.rightmost.dao.TopicMapper;
import com.example.rightmost.dao.UserMapper;
import com.example.rightmost.entity.*;
import com.example.rightmost.service.PieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:javayhch
 * @date:2024/8/26
 * @description:
 */
@Service
public class PieceServiceImpl implements PieceService {

    @Autowired
    public PieceMapper pieceMapper;

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public TopicMapper topicMapper;

    @Autowired
    public PieceLikeMapper pieceLikeMapper;

    @Override
    public List<Piece> getPieceList(String text, String userName, String topicName) {
        return pieceMapper.getPieceList(text, userName, topicName);
    }

    @Override
    public Result getUserNameMap() {
        List<User> users = userMapper.selectAll();
        Map<Integer, String> userNameMap = new HashMap<>();
        for (User user: users) {
            userNameMap.put(user.getId(), user.getUsername());
        }
        return Result.success(userNameMap);
    }

    @Override
    public Result getUserAvatarMap() {
        List<User> users = userMapper.selectAll();
        Map<Integer, String> userNameMap = new HashMap<>();
        for (User user: users) {
            userNameMap.put(user.getId(), user.getAvatar());
        }
        return Result.success(userNameMap);
    }

    @Override
    public Result getTopicNameMap() {
        List<Topic> topics = topicMapper.selectAll();
        Map<Integer, String> topicNameMap = new HashMap<>();
        for (Topic topic: topics) {
            topicNameMap.put(topic.getId(), topic.getName());
        }
        return Result.success(topicNameMap);
    }

    @Override
    public List<Piece> getPieces() {
        return pieceMapper.getPieces();
    }

    @Override
    public Result doWells(Integer pieceId, Integer num) {
        int i = pieceMapper.doWells(pieceId, num);
        if(i > 0){
            return Result.success();
        } else {
            return Result.error("点赞失败");
        }

    }

    @Override
    public Result addPiece(Piece piece) {
        piece.setCreateTime(LocalDateTime.now());
        int i = pieceMapper.addPiece(piece);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("添加失败");
        }
    }

    @Override
    public List<Piece> getPiecesById(Integer id) {
        return pieceMapper.getPiecesById(id);
    }

    @Override
    public Result getLikeInfo(Integer creationId, Integer userId) {
        Integer i = pieceLikeMapper.like(creationId, userId);
        if (i == null){
            return Result.success(false);
        } else {
            return Result.success(true);
        }
    }

    @Override
    public Result doLike(PieceLike pieceLike) {
        pieceLike.setCreateTime(LocalDateTime.now());
        int i = pieceLikeMapper.doLike(pieceLike);
        if (i > 0){
            pieceMapper.updateLikes(pieceLike.getCreationId());
            return Result.success();
        } else {
            return Result.error("失败");
        }
    }

    @Override
    public Result noLike(PieceLike pieceLike) {
        int i = pieceLikeMapper.noLike(pieceLike);
        if (i > 0){
            pieceMapper.updateLikes(pieceLike.getCreationId());
            return Result.success();
        } else {
            return Result.error("失败");
        }
    }

    @Override
    public Piece getPieceById(int pieceId) {
        return pieceMapper.getPieceById(pieceId);
    }

    @Override
    public Map<Integer, User> getUserMap() {
        List<User> users = userMapper.selectAll();
        Map<Integer, User> userMap = new HashMap<>();
        for (User user: users) {
            userMap.put(user.getId(), user);
        }
        return userMap;
    }


}
